//
//  DPAllDegreeIndicateView.m
//  DPEduProject
//
//  Created by 边广生 on 2019/8/23.
//  Copyright © 2019 bianguangsheng. All rights reserved.
//

#import "DPAllDegreeIndicateView.h"
#import "UIView+Additions.h"
#import "DPCommonTool.h"

#define UIColorFromRGBA(rgbValue, alphaValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0x0000FF))/255.0 \
alpha:alphaValue]

#define HexColor(rgbValue) UIColorFromRGBA(rgbValue, 1)
#define UIColorFromRGBA(rgbValue, alphaValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0x00FF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0x0000FF))/255.0 \
alpha:alphaValue]

@interface DPAllDegreeIndicateView()
@property (nonatomic,strong) UIImageView *iconImgv;
@property (nonatomic,strong) UIProgressView *prossView;
@property (nonatomic,assign) DPDegreeType selectType;
@property (nonatomic,strong) UILabel *playLabel;


@end
@implementation DPAllDegreeIndicateView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame settingType:(DPDegreeType)settingType {
    if (self = [super initWithFrame:frame]) {
        //codehere....
        self.selectType = settingType;
        self.backgroundColor = UIColorFromRGBA(0x29282F, 0.5);
        self.layer.cornerRadius = 4;
        [self.layer setMasksToBounds:YES];
        if (settingType == DPDegreeTypeBrightness) {
            //brightness
            [self addSubview:self.iconImgv];
            _iconImgv .image = [UIImage imageNamed:@"亮度调节"];
            self.iconImgv.frame = CGRectMake(12, 8, 16, 16);
            [self addSubview:self.prossView];
            self.prossView.frame = CGRectMake(self.iconImgv.right+8, (32-3)/2, 90, 3);
        }
        if (settingType == DPDegreeTypeVolume) {
            //brightness
            [self addSubview:self.iconImgv];
            _iconImgv .image = [UIImage imageNamed:@"音量调节"];
            self.iconImgv.frame = CGRectMake(12, 8, 16, 16);
            [self addSubview:self.prossView];
            self.prossView.frame = CGRectMake(self.iconImgv.right+8, (32-3)/2, 90, 3);
//            AppDelegate *del = [AppDelegate instance];
//            [self setProgressValue:del.getVolume];
           
        }
        if (settingType == DPDegreeTypePlayProgress) {
            //brightness
            [self addSubview:self.prossView];
           
            [self addSubview:self.playLabel];
             self.prossView.frame = CGRectMake((self.width-90)/2, self.playLabel.bottom+8, 90, 3);
            [self setPlayProgressTextWithCurrentText:@"00:00" TotalText:@"00:00" prossValue:0];
        }
        
    }
    return self;
    
}
- (void)setPlayProgressTextWithCurrentText:(NSString *)curText TotalText:(NSString *)totText prossValue:(CGFloat)pValue{
    if ([DPCommonTool isEmptyForString:curText]) {
        curText = @"00:00";
    }
    if ([DPCommonTool isEmptyForString:totText]) {
        totText = @"00:00";
    }
    
    NSString *secondPart = [NSString stringWithFormat:@"%@%@",@"/",totText];
    NSMutableAttributedString *attstr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@",curText,secondPart]];
    NSDictionary *dic1 = @{NSForegroundColorAttributeName:HexColor(0x00B93B),NSFontAttributeName:[UIFont systemFontOfSize:12]};
    NSDictionary *dic2 = @{NSForegroundColorAttributeName:HexColor(0xFFFFFF),NSFontAttributeName:[UIFont systemFontOfSize:12]};
    NSRange range1 = NSMakeRange(0, curText.length);
    NSRange range2 = NSMakeRange(curText.length,secondPart.length);
    [attstr addAttributes:dic1 range:range1];
    [attstr addAttributes:dic2 range:range2];
    self.playLabel.attributedText = attstr;
    [self.prossView setProgress:pValue];
    
}
- (void)setProgressValue:(CGFloat)value{
//    [self.prossView setProgress:value ];
   
    if (value<0) {
        value = 0;
    }
    NSLog(@"volumevalue:%0.1f",value);
    if (self.selectType == DPDegreeTypeVolume) {
        if (value==0) {
            self.iconImgv.image = [UIImage imageNamed:@"音量调节-关"];
        }else{
            self.iconImgv.image = [UIImage imageNamed:@"音量调节"];
        }
//        AppDelegate *del = [AppDelegate instance];
//        [del setVolume:value];
    }
   
    self.prossView.progress = value;
    
}
- (UIImageView *)iconImgv{
    if (!_iconImgv) {
        _iconImgv = [[UIImageView alloc]init];
        
        _iconImgv.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _iconImgv;
    
}
- (UIProgressView *)prossView{
    if (!_prossView) {
        _prossView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, (32-3)/2, 90, 3)];
        _prossView.progressTintColor = HexColor(0x00B93B);
        _prossView.trackTintColor = HexColor(0xD8D8D8);
        CGFloat bright = [[DPCommonTool sharedTool] getScreenBrightnessValue];
        [_prossView setProgress:bright];
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_prossView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(2, 2)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _prossView.bounds;
        maskLayer.path = maskPath.CGPath;
        _prossView.layer.mask = maskLayer;
    }
    return _prossView;
    
    
}
- (UILabel *)playLabel{
    if (!_playLabel) {
        _playLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 16, 142, 12)];
        _playLabel.font = [UIFont fontWithName:@"PingFangSC" size: 12];
        _playLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _playLabel;
}



@end
